package com.xw.comm.controller;

import com.xw.comm.entity.BaseResponse;
import com.xw.comm.entity.CobraResultCodeConstant;
import com.xw.comm.entity.Common;
import com.xw.comm.entity.ResultCode;
import com.xw.comm.service.SerialService;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.UnsupportedCommOperationException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.TooManyListenersException;

/**
 * 测试步骤：
 * 1、启动端口 start
 * 2、核心数据 core
 *
 * @author xiongwei_vendor
 * @date 2020/12/9 11:37
 */
@Slf4j
@RestController
@RequestMapping("/serial")
@Api(value = "SerialController|数据源数据接口")
public class SerialController {

    @Autowired
    private SerialService serialService;

    @GetMapping(value = "/start")
    @ApiOperation(value = "启动端口 ", notes = "端口com1或者1")
    @ApiImplicitParam(paramType = "query", name = "com", value = "端口", required = false, dataType = "String")
    public BaseResponse start(@RequestParam(value = "com", required = true, defaultValue = "") String com) {
        ResultCode resultCode;
        if (!com.contains(Common.COM_UP) && !com.contains(Common.COM_LOW)) {
            com = Common.COM_UP + com;
        }
        try {
            serialService.start(com);
            resultCode = CobraResultCodeConstant.SUCCESS;
        } catch (NoSuchPortException | PortInUseException | UnsupportedCommOperationException | TooManyListenersException ignored) {
            log.error("启动异常 = ", ignored);
            resultCode = CobraResultCodeConstant.INTERNAL_ERROR;
        }
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/stop")
    @ApiOperation(value = "关闭端口 ", notes = "端口com1或者1")
    @ApiImplicitParam(paramType = "query", name = "com", value = "端口", required = false, dataType = "String")
    public BaseResponse stop(@RequestParam(value = "com", required = true, defaultValue = "") String com) {
        if (!com.contains(Common.COM_UP) && !com.contains(Common.COM_LOW)) {
            com = Common.COM_UP + com;
        }
        serialService.stop(com);
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/sendCharacter")
    @ApiOperation(value = "发送数据", notes = "字符串")
    @ApiImplicitParam(paramType = "query", name = "message", value = "数据,对应键盘的值", required = false, dataType = "String")
    public BaseResponse sendCharacter(@RequestParam(value = "message", required = true, defaultValue = "") String message) {
        serialService.sendMessage(message);
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/sendData")
    @ApiOperation(value = "发送数据", notes = "一个字节")
    @ApiImplicitParam(paramType = "query", name = "message", value = "数据,对应键盘的值", required = false, dataType = "int")
    public BaseResponse sendData(@RequestParam(value = "message", required = true, defaultValue = "") byte message) {
        serialService.sendMessage(new byte[]{message});
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse(resultCode);
    }


    @GetMapping(value = "/core")
    @ApiOperation(value = "核心数据", notes = "字符串")
    public BaseResponse sendCharacter() {
        try {
            serialService.core();
        } catch (InterruptedException e) {
            ResultCode failCode = CobraResultCodeConstant.FAIL;
            new BaseResponse(failCode);
        }
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/sendWindCom")
    @ApiOperation(value = "win调试数据", notes = "字符串")
    public BaseResponse sendWindCom() {
        try {
            serialService.windowsComm();
        } catch (InterruptedException e) {
            ResultCode failCode = CobraResultCodeConstant.FAIL;
            new BaseResponse(failCode);
        }
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse(resultCode);
    }

    @GetMapping(value = "/getCommonList")
    @ApiOperation(value = "获取串口列表", notes = "字符串")
    public BaseResponse<List<String>> getCommonList() {
        try {
            BaseResponse<List<String>> baseResponse = new BaseResponse<List<String>>(CobraResultCodeConstant.SUCCESS);
            baseResponse.setData(serialService.findCommList());
            return baseResponse;
        } catch (InterruptedException e) {
            ResultCode failCode = CobraResultCodeConstant.FAIL;
            new BaseResponse<List<String>>(failCode);
        }
        ResultCode resultCode = CobraResultCodeConstant.SUCCESS;
        return new BaseResponse<List<String>>(resultCode);
    }

}
